package team.archai.nexus.boot.platform.core.granter;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Component;
import team.archai.nexus.boot.auth.model.AuthenticatedInfo;
import team.archai.nexus.boot.auth.util.AuthAssert;
import team.archai.nexus.boot.platform.modules.system.entity.Account;
import team.archai.nexus.boot.platform.modules.system.service.IAccountService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 默认token 获取用户信息实现类
 * @author: lishibin
 * @create: 2020-09-17 20:46
 **/
@Component
public class StandardGranter implements ITokenGranter {

    @Resource
    private IAccountService accountService;

    @Override
    public AuthenticatedInfo grant(AuthParameter authParameter) {
        String authKey = authParameter.getArgs().getStr("authKey");
        String signDate = authParameter.getArgs().getStr("signDate");
        List<String> combineAccount = decryptAuthKey(authKey, signDate);
        String account = combineAccount.get(0);
        AuthenticatedInfo authenticatedInfo = AuthenticatedInfo.builder().build();
        //根据账户获取用户信息
        Account dataEntity = accountService.getOne(Wrappers.<Account>lambdaQuery().eq(Account::getAccount, account));
        AuthAssert.authParam(dataEntity, "User Info Is Not Existence.");
        authenticatedInfo.setSubject(dataEntity.getAccount());
        return authenticatedInfo;
    }
}
